<template>
  <div class="login-container">
    <h2>GroupExercises项目</h2>
    <!--向“/login”发送请求-->
    <div class="form-group">
      <label for="username">用户名:</label>
      <input type="text" id="username" name="username" required v-model="username">
    </div>
    <div class="form-group">
      <label for="password">密码:</label>
      <input type="password" id="password" name="password" required v-model="password">
    </div>
    <div class="register-link">
      <router-link to="/register">没有账号？前往注册</router-link>
    </div>
    <button id="submit" class="submit-btn" @click="login">登录</button>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "IndexView",
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    login(){
      axios.post("http://localhost:8080/login",{
        username: this.username,
        password: this.password
      })
          .then(res => {
            console.log(res.data)
            if (res.data.code === 200) {
              this.$router.push('/home')
            } else {
              alert("登录失败")
            }
          })
          .catch(err => {
            console.log(err)
          })
    }
  }


}
</script>
<style>
.login-container {
  background-color: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-weight: 500;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-weight: 500;
}

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

input[type="text"]:focus, input[type="password"]:focus {
  border-color: #4a90e2;
  outline: none;
}

.register-link {
  text-align: center;
  margin-bottom: 20px;
}

.register-link a {
  color: #4a90e2;
  text-decoration: none;
  font-size: 14px;
}

.register-link a:hover {
  text-decoration: underline;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background-color: #4a90e2;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #357abd;
}
</style>


